package com.aididi.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Auther: zx.xu
 * @Description:
 */
@Configuration
@EnableTransactionManagement
@Slf4j
public class DruidDataSourceConfig {

    @Autowired
    private DruidDataSourceSettings druidDataSourceSettings;

    public static String DRIVER_CLASSNAME;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    /**
     * @Description:    使用alibaba druid数据源
     * @Author zx.xu
     * @Param: []
     */
    @Bean(name="dataSource1")
    public DataSource dataSource1() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(druidDataSourceSettings.getDriverClassName());
        DRIVER_CLASSNAME = druidDataSourceSettings.getDriverClassName();
        druidDataSource.setUrl(druidDataSourceSettings.getUrl());
        druidDataSource.setUsername(druidDataSourceSettings.getUsername());
        druidDataSource.setPassword(druidDataSourceSettings.getPassword());
        druidDataSource.setInitialSize(druidDataSourceSettings.getInitialSize());
        druidDataSource.setMinIdle(druidDataSourceSettings.getMinIdle());
        druidDataSource.setMaxActive(druidDataSourceSettings.getMaxActive());
        druidDataSource.setTimeBetweenEvictionRunsMillis(druidDataSourceSettings.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(druidDataSourceSettings.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(druidDataSourceSettings.getValidationQuery());
        druidDataSource.setTestWhileIdle(druidDataSourceSettings.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(druidDataSourceSettings.isTestOnBorrow());
        druidDataSource.setTestOnReturn(druidDataSourceSettings.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(druidDataSourceSettings.isPoolPreparedStatements());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidDataSourceSettings.getMaxPoolPreparedStatementPerConnectionSize());
        druidDataSource.setFilters(druidDataSourceSettings.getFilters());
        druidDataSource.setConnectionProperties(druidDataSourceSettings.getConnectionProperties());
        log.info("druid datasource config : {}",druidDataSource);
        return druidDataSource;
    }


    /**
     * @Description: 事务管理
     * @Author zx.xu
     * @Param: []
     */
    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource1());
        return transactionManager;
    }

}
